package com.blb.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.community.Annotation.Log;
import com.blb.community.entity.SysRole;
import com.blb.community.entity.vo.RoleMenuVO;
import com.blb.community.entity.vo.RoleVO;
import com.blb.community.service.ISysRoleService;
import com.blb.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Api(tags = "角色类型管理模块")
@RestController
@RequestMapping("/sys-role")
public class SysRoleController {
    @Autowired
    private ISysRoleService sysRoleService;

    @ApiOperation("模糊查询")
    @PostMapping("/roles/page")
    public ResponseResult<IPage<SysRole>> getAllRoleList(RoleVO roleVO) {
        IPage<SysRole> allRoleList = sysRoleService.getAllRoleList(roleVO, new Page<>(roleVO.getCurrent(), 5L));
        return ResponseResult.ok(allRoleList);
    }


    @ApiOperation("查询角色下拉")
    @GetMapping
    public ResponseResult<List<SysRole>> getRolePage() {
        List<SysRole> list = sysRoleService.list(new QueryWrapper<SysRole>().lambda().notIn(SysRole::getRoleId, 1));
        return ResponseResult.ok(list);
    }

    @ApiOperation("添加角色")
    @PostMapping("/role")
    @Log(module = "添加角色", operationType = 1, requestMethod = "POST")
    public ResponseResult<String> addRole(@RequestBody RoleMenuVO roleMenuVO) {
        List<Integer> menus = roleMenuVO.getMenus();
        roleMenuVO.setRoleSort(2);
        roleMenuVO.setStatus("0");
        sysRoleService.addRole(roleMenuVO);
        return ResponseResult.ok("添加成功");
    }

    @ApiOperation("更新角色")
    @PutMapping("/role")
    @Log(module = "更新角色", operationType = 2, requestMethod = "PUT")
    public ResponseResult<String> updateRoel(@RequestBody RoleMenuVO roleMenuVO) {

        sysRoleService.updateById(roleMenuVO);
        return ResponseResult.ok("更新成功");
    }

    @Log(module = "删除角色", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("删除角色")
    @DeleteMapping("/role/{RoleId}")
    public ResponseResult<String> delRole(@PathVariable Long RoleId) {
        sysRoleService.delRole(RoleId);
        return ResponseResult.ok("删除成功");
    }

    @Log(module = "批量删除字典类型信息", operationType = 3, requestMethod = "DELETE")
    @DeleteMapping("/delrole/{roleIds}")
    public ResponseResult<String> delAll(@PathVariable ArrayList<String> roleIds) {
        sysRoleService.removeByIds(roleIds);
        return ResponseResult.ok("删除成功");
    }

    @Log(module = "修改角色", operationType = 2, requestMethod = "PUT")
    @PutMapping("/status")
    public ResponseResult<String> updateStatus(@RequestBody RoleMenuVO roleMenuVO) {
        sysRoleService.updateById(roleMenuVO);
        return ResponseResult.ok("更新成功");
    }
}

